package com.myspring.lab.control;

import com.myspring.lab.dao.DbConnnect;
import com.myspring.lab.dao.DbUser;
import com.myspring.lab.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;


@Controller
public class doUser {
    @RequestMapping(value = "/user")
    public String user(Model model){
        DbUser dbUser = new DbUser();
        User [] users = dbUser.getAllUser();
        model.addAttribute("users",users);
        return "user";
    }
    @RequestMapping(value = "/deleteuser",method = RequestMethod.GET)
    public String deleteUser(String username,String password){
        DbUser dbUser = new DbUser();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        dbUser.deleteUser(user);
        return "redirect:user";
    }

    @RequestMapping(value = "/adduser", method = RequestMethod.GET)
    public String addUser(String username, String password){
        DbUser dbUser = new DbUser();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        dbUser.addUser(user);
        return "redirect:user";
    }

    @RequestMapping(value = "/edituser",method = RequestMethod.GET)
    public String editUser(HttpServletRequest request){
        DbUser dbUser = new DbUser();
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        dbUser.modifyUser(user);
        return "redirect:user";
    }

    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public String registUser(HttpServletRequest request, Model model){
        DbConnnect dbConnnect = new DbConnnect();
        DbUser dbUser = new DbUser();
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        if(dbUser.addUser(user)){
            model.addAttribute("status","注册成功，请登录");
        }else{
            model.addAttribute("status","注册失败，请更换用户名");
        }
        return "regist.html";
    }

}
